import { Component, ViewChild, ComponentFactoryResolver, Input, OnInit, ViewContainerRef, SimpleChanges } from '@angular/core';
import { EventService } from '../../shared/service/event.service';
import { TabsService } from '../../shared/service/tabs.service';
import { ComItem } from './com-item';
// tslint:disable-next-line:max-line-length
@Component({ selector: 'app-secondfixedtab', template: '<section>\n' + '<ng-template #secondefixedComponent></ng-template>\n' + '</section>' })
export class FixedTabComsComponent implements OnInit {
    @Input() subComs: ComItem[];
    @ViewChild('secondefixedComponent', { read: ViewContainerRef }) myRef: ViewContainerRef;
    // tslint:disable-next-line:max-line-length
    constructor(private componentFactoryResolver: ComponentFactoryResolver, private eventService: EventService, public tabService: TabsService) { }
    ngOnInit() {
        if (this.eventService.eventEmit.listenerCount('refreshFixedTabContainer') > 0) {
            this.eventService.eventEmit.removeListener('refreshFixedTabContainer');
        }
        this.eventService.eventEmit.on('refreshFixedTabContainer', () => {
            this.refreshComponent();
        });
    }
    // tslint:disable-next-line:use-life-cycle-interface
    ngOnChanges(changes: SimpleChanges): void { this.myRef.clear(); this.loadComponent(); }
    refreshComponent() {
        const that = this;
        if (localStorage.getItem("dataMapFixedTabContainer")) {
            const storeTagData = JSON.parse(localStorage.getItem("dataMapFixedTabContainer"));
            // setTimeout(() => {
            this.subComs = [];
            const cDataObj = storeTagData.currentData;
            const tags = cDataObj.tags;
            if (tags !== undefined && tags.length > 0) {
                const len = tags.length;
                for (let i = 0; i < len; i++) {
                    const tag = tags[i];
                    if (tag.linearLayout === 'body') {
                        this.tabService.getComsDataByStyle(tag.style, callData => {
                            if (callData) {
                                this.subComs.push(callData);
                                if (len - 1 === i) {
                                    if (!localStorage.getItem('refreshFooterComs')) {
                                        localStorage.setItem("refreshFooterComsTag", JSON.stringify({ clean: true }));
                                        // tslint:disable-next-line:max-line-length
                                        this.eventService.eventEmit.emit("testRefreshFooterComsContainer", "testRefreshFooterComsContainer");
                                    }
                                }
                            }
                        }, tag);
                    } else {
                        localStorage.setItem("refreshFooterComsTag", JSON.stringify(tag));
                        this.eventService.eventEmit.emit("testRefreshFooterComsContainer", "testRefreshFooterComsContainer");
                    }
                }
            }
            this.myRef.clear();
            setTimeout(() => {
                localStorage.removeItem("dataMapFixedTabContainer");
                if (this.subComs !== undefined) {
                    const l = this.subComs.length;
                    for (let j = 0; j < l; j++) {
                        const v = this.subComs[j];
                        const factory = this.componentFactoryResolver.resolveComponentFactory(v.component);
                        const ref = this.myRef.createComponent(factory);
                        ref.instance.tagData = v.tagData;
                        ref.changeDetectorRef.detectChanges();
                    }
                }
            }, 50);
            // }, 50);
        }
    }
    loadComponent() {
        this.myRef.clear();
        const len = this.subComs.length;
        for (let i = 0; i < len; i++) {
            const v = this.subComs[i];
            const factory = this.componentFactoryResolver.resolveComponentFactory(v.component);
            const ref = this.myRef.createComponent(factory);
            ref.instance.tagData = v.tagData;
        }
    }
}
